package com.prs.moa.usermanager.service.impl;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.prs.moa.usermanager.dao.UserInfoDAO;
import com.prs.moa.usermanager.model.UserDTO;
import com.prs.moa.usermanager.model.UserInfo;
import com.prs.moa.usermanager.service.UserInfoService;
import com.prs.um.deptmanager.dto.Dept;
import com.prs.um.dutymanager.dto.Duty;
import com.prs.um.usermanager.dao.UserDAO;
import com.prs.um.usermanager.dto.User;
import com.prs.util.MD5Util;

@Service
@Transactional(rollbackFor = Exception.class)
public class UserInfoServcieImpl implements UserInfoService{
	
	@Resource
	private UserInfoDAO userInfoDAO;
	
	@Resource
	private UserDAO userDAO;

	/*
	 * @Description: 添加用户
	 * @author flybonew@163.com
	 * @date 2013-1-9 下午5:46:47 
	 */
	@Override
	public boolean addUser(UserDTO userDTO) {
		
		User user = createNewUserInstance(userDTO);
		if(null != user.getId() && !"".equals(user.getId())){
			//添加用户信息
			UserInfo userInfo = createNewUserInfo(user.getId(), userDTO);
			this.userInfoDAO.addUserInfo(userInfo);
			return true;
		}
		return false;
	}
	
	/*
	 * @Description: 验证登录 
	 * @author flybonew@163.com
	 * @date 2013-1-10 下午2:17:15 
	 */
	@Override
	public boolean validateUser(UserDTO userDTO) {
		User user = this.userDAO.seleteUserByLoginId(userDTO.getLoginId());
		if(null != user){
			String password = MD5Util.getMd5Str(userDTO.getPassWord());
			if(user.getPassword().equals(password)){
				return true;
			}
		}
		return false;
	}
	
	/*
	 * 创建一个UM用户
	 */
	private User createNewUserInstance(UserDTO userDTO){
		User user = new User();
		user.setLoginId(userDTO.getLoginId());
		user.setPassword(MD5Util.getMd5Str(userDTO.getPassWord()));
		user.setState(1);
		user.setUsername(userDTO.getUserName());
		if(null != userDTO.getSupervisor() && !"".equals(userDTO.getSupervisor())){
			User supervisor = new User();
			supervisor.setId(userDTO.getSupervisor());
			user.setSupervisor(supervisor);
		}
		if(null != userDTO.getDept() && !"".equals(userDTO.getDept())){
			String[] deptList = userDTO.getDept().split(",");
			Set<Dept> depts = new HashSet<Dept>();
			for (int i = 0; i < deptList.length; i++) {
				Dept dept = new Dept();
				dept.setId(deptList[i]);
				depts.add(dept);
			}
			user.setDeptList(depts);
		}
		if(null != userDTO.getDuty() && !"".equals(userDTO.getDuty())){
			String[] dutyList = userDTO.getDuty().split(",");
			Set<Duty> duties = new HashSet<Duty>();
			for (int i = 0; i < dutyList.length; i++) {
				Duty duty = new Duty();
				duty.setId(dutyList[i]);
				duties.add(duty);
			}
			user.setDutyList(duties);
		}
		user = this.userDAO.addUser(user);
		return user;
	}
	
	private UserInfo createNewUserInfo(String userId,UserDTO userDTO){
		UserInfo userInfo = new UserInfo();
		userInfo.setId(userId);
		userInfo.setAddress(userDTO.getAddress());
		userInfo.setBitrhday(userDTO.getBitrhday());
		userInfo.setEmail(userDTO.getEmail());
		userInfo.setMobile(userDTO.getMobile());
		userInfo.setMsn(userDTO.getMsn());
		userInfo.setName(userDTO.getUserName());
		userInfo.setPhone(userDTO.getPhone());
		userInfo.setQq(userDTO.getQq());
		userInfo.setSex(userDTO.getSex());
		userInfo.setShortNum(userDTO.getShortNum());
		return userInfo;
	}

}
